using Microsoft.Phone.Controls.Primitives;
using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Navigation;
namespace Microsoft.Phone.Controls
{
	[TemplatePart(Name = "DateTimeButton", Type = typeof(ButtonBase))]
	public class DateTimePickerBase : Control
	{
		private const string ButtonPartName = "DateTimeButton";
		private ButtonBase _dateButtonPart;
		private PhoneApplicationFrame _frame;
		private object _frameContentWhenOpened;
		private NavigationInTransition _savedNavigationInTransition;
		private NavigationOutTransition _savedNavigationOutTransition;
		private IDateTimePickerPage _dateTimePickerPage;
		public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(DateTime?), typeof(DateTimePickerBase), new PropertyMetadata(null, new PropertyChangedCallback(DateTimePickerBase.OnValueChanged)));
		public static readonly DependencyProperty ValueStringProperty = DependencyProperty.Register("ValueString", typeof(string), typeof(DateTimePickerBase), null);
		public static readonly DependencyProperty ValueStringFormatProperty = DependencyProperty.Register("ValueStringFormat", typeof(string), typeof(DateTimePickerBase), new PropertyMetadata(null, new PropertyChangedCallback(DateTimePickerBase.OnValueStringFormatChanged)));
		public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(object), typeof(DateTimePickerBase), null);
		public static readonly DependencyProperty HeaderTemplateProperty = DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(DateTimePickerBase), null);
		public static readonly DependencyProperty PickerPageUriProperty = DependencyProperty.Register("PickerPageUri", typeof(Uri), typeof(DateTimePickerBase), null);
		public event EventHandler<DateTimeValueChangedEventArgs> ValueChanged;
		[TypeConverter(typeof(TimeTypeConverter))]
		public DateTime? Value
		{
			get
			{
				return (DateTime?)base.GetValue(DateTimePickerBase.ValueProperty);
			}
			set
			{
				base.SetValue(DateTimePickerBase.ValueProperty, value);
			}
		}
		public string ValueString
		{
			get
			{
				return (string)base.GetValue(DateTimePickerBase.ValueStringProperty);
			}
			private set
			{
				base.SetValue(DateTimePickerBase.ValueStringProperty, value);
			}
		}
		public string ValueStringFormat
		{
			get
			{
				return (string)base.GetValue(DateTimePickerBase.ValueStringFormatProperty);
			}
			set
			{
				base.SetValue(DateTimePickerBase.ValueStringFormatProperty, value);
			}
		}
		public object Header
		{
			get
			{
				return base.GetValue(DateTimePickerBase.HeaderProperty);
			}
			set
			{
				base.SetValue(DateTimePickerBase.HeaderProperty, value);
			}
		}
		public DataTemplate HeaderTemplate
		{
			get
			{
				return (DataTemplate)base.GetValue(DateTimePickerBase.HeaderTemplateProperty);
			}
			set
			{
				base.SetValue(DateTimePickerBase.HeaderTemplateProperty, value);
			}
		}
		public Uri PickerPageUri
		{
			get
			{
				return (Uri)base.GetValue(DateTimePickerBase.PickerPageUriProperty);
			}
			set
			{
				base.SetValue(DateTimePickerBase.PickerPageUriProperty, value);
			}
		}
		protected virtual string ValueStringFormatFallback
		{
			get
			{
				return "{0}";
			}
		}
		private static void OnValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			((DateTimePickerBase)o).OnValueChanged((DateTime?)e.get_OldValue(), (DateTime?)e.get_NewValue());
		}
		private void OnValueChanged(DateTime? oldValue, DateTime? newValue)
		{
			this.UpdateValueString();
			this.OnValueChanged(new DateTimeValueChangedEventArgs(oldValue, newValue));
		}
		protected virtual void OnValueChanged(DateTimeValueChangedEventArgs e)
		{
			EventHandler<DateTimeValueChangedEventArgs> valueChanged = this.ValueChanged;
			if (valueChanged != null)
			{
				valueChanged.Invoke(this, e);
			}
		}
		private static void OnValueStringFormatChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			((DateTimePickerBase)o).OnValueStringFormatChanged();
		}
		private void OnValueStringFormatChanged()
		{
			this.UpdateValueString();
		}
		public override void OnApplyTemplate()
		{
			if (this._dateButtonPart != null)
			{
				this._dateButtonPart.remove_Click(new RoutedEventHandler(this.OnDateButtonClick));
			}
			base.OnApplyTemplate();
			this._dateButtonPart = (base.GetTemplateChild("DateTimeButton") as ButtonBase);
			if (this._dateButtonPart != null)
			{
				this._dateButtonPart.add_Click(new RoutedEventHandler(this.OnDateButtonClick));
			}
		}
		private void OnDateButtonClick(object sender, RoutedEventArgs e)
		{
			this.OpenPickerPage();
		}
		private void UpdateValueString()
		{
			this.ValueString = string.Format(CultureInfo.get_CurrentCulture(), this.ValueStringFormat ?? this.ValueStringFormatFallback, new object[]
			{
				this.Value
			});
		}
		private void OpenPickerPage()
		{
			if (null == this.PickerPageUri)
			{
				throw new ArgumentException("PickerPageUri property must not be null.");
			}
			if (this._frame == null)
			{
				this._frame = (Application.get_Current().get_RootVisual() as PhoneApplicationFrame);
				if (this._frame != null)
				{
					this._frameContentWhenOpened = this._frame.get_Content();
					UIElement uIElement = this._frameContentWhenOpened as UIElement;
					if (uIElement != null)
					{
						this._savedNavigationInTransition = TransitionService.GetNavigationInTransition(uIElement);
						TransitionService.SetNavigationInTransition(uIElement, null);
						this._savedNavigationOutTransition = TransitionService.GetNavigationOutTransition(uIElement);
						TransitionService.SetNavigationOutTransition(uIElement, null);
					}
					this._frame.add_Navigated(new NavigatedEventHandler(this.OnFrameNavigated));
					this._frame.add_NavigationStopped(new NavigationStoppedEventHandler(this.OnFrameNavigationStoppedOrFailed));
					this._frame.add_NavigationFailed(new NavigationFailedEventHandler(this.OnFrameNavigationStoppedOrFailed));
					this._frame.Navigate(this.PickerPageUri);
				}
			}
		}
		private void ClosePickerPage()
		{
			if (this._frame != null)
			{
				this._frame.remove_Navigated(new NavigatedEventHandler(this.OnFrameNavigated));
				this._frame.remove_NavigationStopped(new NavigationStoppedEventHandler(this.OnFrameNavigationStoppedOrFailed));
				this._frame.remove_NavigationFailed(new NavigationFailedEventHandler(this.OnFrameNavigationStoppedOrFailed));
				UIElement uIElement = this._frameContentWhenOpened as UIElement;
				if (uIElement != null)
				{
					TransitionService.SetNavigationInTransition(uIElement, this._savedNavigationInTransition);
					this._savedNavigationInTransition = null;
					TransitionService.SetNavigationOutTransition(uIElement, this._savedNavigationOutTransition);
					this._savedNavigationOutTransition = null;
				}
				this._frame = null;
				this._frameContentWhenOpened = null;
			}
			if (this._dateTimePickerPage != null)
			{
				if (this._dateTimePickerPage.Value.get_HasValue())
				{
					this.Value = new DateTime?(this._dateTimePickerPage.Value.get_Value());
				}
				this._dateTimePickerPage = null;
			}
		}
		private void OnFrameNavigated(object sender, NavigationEventArgs e)
		{
			if (e.get_Content() == this._frameContentWhenOpened)
			{
				this.ClosePickerPage();
				return;
			}
			if (this._dateTimePickerPage == null)
			{
				this._dateTimePickerPage = (e.get_Content() as IDateTimePickerPage);
				if (this._dateTimePickerPage != null)
				{
					this._dateTimePickerPage.Value = new DateTime?(this.Value.GetValueOrDefault(DateTime.get_Now()));
				}
			}
		}
		private void OnFrameNavigationStoppedOrFailed(object sender, EventArgs e)
		{
			this.ClosePickerPage();
		}
	}
}
